<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>let关键字的使用</title>
  </head>

  <body>

  </body>
  <script>
    /* 
   let关键字的使用：
     1.let取消预解析，变量声明不会提升，必须先声明然后再使用
     2.let不可以重复定义
     3.let有块级作用域
     4.解决for循环里的变量污染
     5.let不影响作用域链
   */
    //1. let取消预解析， 变量声明不会提升， 必须先声明然后再使用
    //console.log(str); //Uncaught ReferenceError: Cannot access 'str' before initialization
    let str = "hello";

    // 2. let不可以重复定义
    let str2 = "world";
    //let str2 = "jason";
    //console.log(str2); //Uncaught SyntaxError: Identifier 'str2' has already been declared

    //3.let有块级作用域
    {
      let num = 10;
    }
    //console.log(num); //Uncaught ReferenceError: num is not defined

    //4.解决for循环里的变量污染
    for (let i = 0; i < 4; i++) {
      console.log(i); //0 1 2 3
    }
    //console.log(i); //Uncaught ReferenceError: i is not defined

    //5.let不影响作用域链
    let school = "atguigu"; {
      let school = "尚硅谷";

      function fn() {
        console.log(school); //"尚硅谷"
      }
      fn();
    }
    console.log(school); //"atguigu"
  </script>

</html>